//Copyright 2011 New York City 3D Community

//This file is part of New York City 3D.

//New York City 3D is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//New York City 3D is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with New York City 3D.  If not, see <http://www.gnu.org/licenses/>.

package nyc3d.street;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;

/**
 * A sidewalk for pedestrians
 */
public class Sidewalk {

    Node pivotNode;
    Geometry geometry;
    Material material;
    RigidBodyControl control;

    public Sidewalk(Node parentNode, String name, String texture, Vector3f center, Vector3f rotation, Vector3f scale, AssetManager assetManager, PhysicsSpace pSpace){
        pivotNode = new Node(name + "_PivotNode");
        Box box = new Box(Float.valueOf(scale.x) / 2f, Float.valueOf(scale.y) / 2f, Float.valueOf(scale.z) / 2f);

        geometry = new Geometry(name, box);
        geometry.setShadowMode(ShadowMode.Receive);

        geometry.setLocalTranslation(new Vector3f(Float.valueOf(center.x), Float.valueOf(center.y), Float.valueOf(center.z)));

        Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/" + texture));
        mat.setFloat("Shininess", 5f);
        geometry.setMaterial(mat);
        pivotNode.attachChild(geometry);
        pivotNode.rotate(Float.valueOf(rotation.x), Float.valueOf(rotation.y), Float.valueOf(rotation.z));

        control = new RigidBodyControl(new BoxCollisionShape(new Vector3f(Float.valueOf(scale.x) / 2f, Float.valueOf(scale.y) / 2f, Float.valueOf(scale.z) / 2f)), 0);

        parentNode.attachChild(pivotNode);
        Quaternion q1 = new Quaternion();
        q1.fromAngleAxis(Float.valueOf(rotation.y), Vector3f.UNIT_Y);
        control.setSpatial(geometry);
        control.setKinematic(true);
        pSpace.add(control);
    }

    public static void buildLinear(Node parentNode, String name, String texture, Vector3f center, Vector3f endCenter, Vector3f rotation, Vector3f scale, AssetManager assetManager, PhysicsSpace pSpace){
        float xDifference = endCenter.x - center.x, yDifference = endCenter.y - center.y, zDifference = endCenter.z - center.z, xRatio = 1, yRatio = xDifference / xRatio, zRatio = zDifference / xRatio;

        if (xDifference > yDifference && yDifference > zDifference){
            xRatio = 1;
            yRatio = yDifference / xDifference;
            zRatio = zDifference / xDifference;
        }
        else if(zDifference>xDifference && zDifference>yDifference){
            zRatio = 1;
            xRatio = xDifference / zDifference;
            yRatio = yDifference / zDifference;
        }
        else{
            yRatio = 1;
            xRatio = xDifference / yDifference;
            zRatio = zDifference / yDifference;
        }


        for(float centerX = center.x, centerY = center.y, centerZ = center.z; centerX <= endCenter.x && centerY <= endCenter.y && centerZ <= endCenter.z; centerX += scale.x * xRatio , centerY += scale.y * yRatio, centerZ += scale.z * zRatio){
            Sidewalk s = new Sidewalk(parentNode, name, texture, new Vector3f(centerX, centerY, centerZ), rotation, scale, assetManager, pSpace);
        }   
    }
}